//
//  CGProvinceViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/5.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGProvinceViewController.h"
#import "AppDelegate.h"
@interface CGProvinceViewController ()
@property(nonatomic,strong)NSArray *provinces;
@end

@implementation CGProvinceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"选择省份";
    
    [self initTableView];
    [self fetchAllProvince];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }

}

-(void)fetchAllProvince{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"0";
    
    ZYHRequest *request = [ZYHRequest new];
    [request getAsyncEncryptRequest:@"/main/getAllProvince.app?" parameters:parameters success:^(id responseData) {
        
        NSLog(@"省份信息请求成功!!");
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        if (result.length == 0) {
            return ;
        }
         //把接收到的数组对象变成model
        self.provinces = [CGProvince objectArrayWithJSONData:responseData];
       
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"省份信息请求失败:%@",error);
    }];
}

#pragma mark overide UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.provinces.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"tableviewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    CGProvince *province = self.provinces[indexPath.row];
    cell.textLabel.text = province.province;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   
//    CGHomePageViewController *homeVC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
//    homeVC.cityName = province.province;
//    [self.navigationController popToViewController:homeVC animated:YES];

    CGProvince *province = self.provinces[indexPath.row];
    CGCityViewController *cityVC = [CGCityViewController new];
    cityVC.myProvince = province;
    [self.navigationController pushViewController:cityVC animated:YES];
}

//UITableViewCell左侧会有默认15像素的空白,使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}



@end
